package RestrantRecommend;

import java.util.ArrayList;
import java.util.List;

import com.travel.core.AbstractDao;

public class TouristRestrantRecommend {

	private ShopSearch shopsearch;

	public TouristRestrantRecommend() {
	}

	public TouristRestrantRecommend(ShopSearch shopsearch) {
		this.shopsearch = shopsearch;
	}

	public List<shop> doTouristRestrantRecommed() {
		List<shop> shops = new ArrayList<shop>();
		List<String> category = shopsearch.getCategory();
		List<String> distinct = shopsearch.getDistinct();
		List<String> star = shopsearch.getStar();
		List<String> guize = new ArrayList<String>();
		String sort = shopsearch.getSort();
		String cate = "";
		String dis = "";
		String sta = "";
		String search = "";
		if ((category.size() == 0) || (category.size() == 1 && category.get(0).equals("不限"))) {
			cate = cate + "";
		} else {
			String s = "";
			for (String ca : category) {
				s = s + "'" + ca + "',";
			}
			s = s.substring(0, s.length() - 1);
			cate = "category in(" + s + ")";
		}
		if ((distinct.size() == 0) || (distinct.size() == 1 && distinct.get(0).equals("不限"))) {
			dis = dis + "";
		} else {
			String d = "";
			for (String di : distinct) {
				d = d + "'" + di + "',";
			}
			d = d.substring(0, d.length() - 1);
			dis = "district in(" + d + ")";
		}
		if ((star.size() == 0) || (star.size() == 1 && star.get(0).equals("不限"))) {
			sta = sta + "";
		} else {
			String a = "";
			for (String st : star) {
				a = a + "'" + st + "',";
			}
			// System.out.println(a);
			a = a.substring(0, a.lastIndexOf(","));
			// a.substring(0, a.length()-1);
			sta = "level in(" + a + ")";
		}
		if (sort.equals("不限")) {
			search = "";
		}
		if (sort.equals("好评")) {
			search = "order by (taste+environment+service) desc";
		}
		if (sort.equals("人气")) {
			search = "order by comment desc";
		}
		if (sort.equals("最佳口味")) {
			search = "order by taste desc";
		}
		if (sort.equals("最佳环境")) {
			search = "order by environment desc";
		}
		if (sort.equals("最佳服务")) {
			search = "order by service desc";
		}
		if (sort.equals("人均消费")) {
			search = "order by price asc";
		}
		guize.add(cate);
		guize.add(dis);
		guize.add(sta);
		guize.add(search);
		String sql = "select * from shop";
		if (cate.length() > 0) {
			sql = sql + " where " + cate;
			for (int i = 1; i < 3; i++) {
				if (guize.get(i).length() > 0) {
					sql = sql + " and " + guize.get(i);
				}
			}
			sql = sql + " " + search;
		} else if (cate.length() == 0 && dis.length() > 0) {
			sql = sql + " where " + dis;
			if (sta.length() > 0) {
				sql = sql + " and " + sta + " " + search;
			}
		} else if (cate.length() == 0 && dis.length() == 0 && sta.length() > 0) {
			sql = sql + " where " + sta + " " + search;
		} else if (cate.length() == 0 && dis.length() == 0 && sta.length() == 0 && search.length() > 0) {
			sql = sql + " " + search;
		}
		System.out.println(sql);
		shops = AbstractDao.getShops(sql);
		return shops;
	}

    // 测试，TODO 尽量写单元测试
	public static void main(String[] args) {
		List<String> category = new ArrayList<String>();
		/*
		 * category.add("广西菜"); category.add("粤菜");
		 */
		List<String> distinct = new ArrayList<String>();
		distinct.add("七星区");
		distinct.add("中心商城");
		List<String> star = new ArrayList<String>();
		star.add("准四星商户");
		String sort = "最佳环境";
		ShopSearch shopsearch = new ShopSearch(category, distinct, star, sort);
		List<shop> shops = new TouristRestrantRecommend(shopsearch).doTouristRestrantRecommed();
		for (shop s : shops) {
			System.out.println(s.getShopname() + "   " + s.getCategory() + "   " + s.getDistrict());
		}
	}
}
